﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Windows.Controls.Ribbon;
using VRacer;
using System.Windows.Input;
using System.Windows;
using System.Windows.Media.Imaging;

namespace VRacer.Controls.CommandsNS
{
    public static class MyRibbonCommands
    {
        public static readonly RibbonCommand StartGame;
        public static readonly RibbonCommand EndGame;
        public static readonly RibbonCommand AddNewComputerPlayer;
        public static readonly RibbonCommand AddNewHumanPlayer;

        public static readonly RibbonCommand CloseApp;
        public static readonly ICommand CMD;

        private class temp : ICommand
        {

            #region ICommand Members

            public bool CanExecute(object parameter)
            {
                return true;
            }

            public event EventHandler CanExecuteChanged;

            public void Execute(object parameter)
            {
                if (App.Current.MainWindow.WindowState != WindowState.Maximized)
                {
                    App.Current.MainWindow.WindowStyle = WindowStyle.None;
                    App.Current.MainWindow.WindowState = WindowState.Maximized;
                }
                else
                {
                    App.Current.MainWindow.WindowStyle = WindowStyle.SingleBorderWindow;
                    App.Current.MainWindow.WindowState = WindowState.Normal;
                }
            }

            #endregion
        }

        static MyRibbonCommands()
        {
            CMD = new temp();

            AddNewHumanPlayer = new RibbonCommand()
            {
                LabelTitle = "Add player",
                LargeImageSource = new BitmapImage(new Uri(@";component/Icons/add_user.png", UriKind.Relative)),
                SmallImageSource = new BitmapImage(new Uri(@";component/Icons/add_user.png", UriKind.Relative)),
                ToolTipTitle = "Add player",
                ToolTipDescription = "Adds new player controlled by human to the game."
            };
            AddNewHumanPlayer.CanExecute += new System.Windows.Input.CanExecuteRoutedEventHandler(AddNewHumanPlayer_CanExecute);
            AddNewHumanPlayer.Executed += new System.Windows.Input.ExecutedRoutedEventHandler(AddNewHumanPlayer_Executed);

            AddNewComputerPlayer = new RibbonCommand()
            {
                LabelTitle = "Add computer",
                LargeImageSource = new BitmapImage(new Uri(@";component/Icons/system_add.png", UriKind.Relative)),
                SmallImageSource = new BitmapImage(new Uri(@";component/Icons/system_add.png", UriKind.Relative)),
                ToolTipTitle = "Add computer",
                ToolTipDescription = "Add new player controlled by computer to the game."
            };
            AddNewComputerPlayer.CanExecute += new CanExecuteRoutedEventHandler(AddNewHumanPlayer_CanExecute);
            AddNewComputerPlayer.Executed += new ExecutedRoutedEventHandler(AddNewComputerPlayer_Executed);

            CloseApp = new RibbonCommand()
            {
                LabelTitle = "Exit",
                LargeImageSource = new BitmapImage(new Uri(@";component/Icons/editdelete.png", UriKind.Relative)),
                SmallImageSource = new BitmapImage(new Uri(@";component/Icons/editdelete.png", UriKind.Relative))
            };
            CloseApp.CanExecute += (object sender, CanExecuteRoutedEventArgs e) => e.CanExecute = true;
            CloseApp.Executed += (object sender, ExecutedRoutedEventArgs e) => Application.Current.Shutdown();

            EndGame = new RibbonCommand()
            {
                LabelTitle = "End",
                LargeImageSource = new BitmapImage(new Uri(@";component/Icons/player_stop.png", UriKind.Relative)),
                SmallImageSource = new BitmapImage(new Uri(@";component/Icons/player_stop.png", UriKind.Relative)),
                ToolTipTitle = "Ends the game",
                ToolTipDescription = "..."
            };
            EndGame.CanExecute += new CanExecuteRoutedEventHandler(EndGame_CanExecute);
            EndGame.Executed += new ExecutedRoutedEventHandler(EndGame_Executed);

            StartGame = new RibbonCommand()
            {
                LabelTitle = "Start",
                LargeImageSource = new BitmapImage(new Uri(@";component/Icons/1rightarrow.png", UriKind.Relative)),
                SmallImageSource = new BitmapImage(new Uri(@";component/Icons/1rightarrow.png", UriKind.Relative)),
                ToolTipTitle = "Starts the game",
                ToolTipDescription = "..."
            };
            StartGame.CanExecute += new CanExecuteRoutedEventHandler(StartGame_CanExecute);
            StartGame.Executed += new ExecutedRoutedEventHandler(StartGame_Executed);


        }

        static void StartGame_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            Business.Instance.Game.Initialize();
            Business.Instance.Page = Urls.Game;
        }

        static void StartGame_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            var game = Business.Instance.Game;
            e.CanExecute = game != null && game.State == VRacer.GameState.Uninitialized && game.Players.Count>0;
        }

        static void EndGame_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            var game = Business.Instance.Game;
            e.CanExecute = game != null && game.State == VRacer.GameState.Started;
        }

        static void EndGame_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            Business.Instance.Game.End();
            Business.Instance.Page = Urls.MainMenu;
        }

        static void AddNewComputerPlayer_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            Business.Instance.Game.Add(new AIPlayer(Business.Instance.Game));
        }

        static void AddNewHumanPlayer_Executed(object sender, System.Windows.Input.ExecutedRoutedEventArgs e)
        {
            Business.Instance.Game.Add(new HumanPlayer());
        }

        static void AddNewHumanPlayer_CanExecute(object sender, System.Windows.Input.CanExecuteRoutedEventArgs e)
        {
            var game = Business.Instance.Game;
            e.CanExecute = game != null && game.State == VRacer.GameState.Uninitialized;
        }

        
    }
}
